package zheng.mockit;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Created by zcz on 16-7-29.
 */
public class DefaultMessageHandler implements MessageHandler {

    // message from testcase to plugin (only for server plugin)
    private BlockingQueue<Object> testCaseMessageQueue;

    // message from plugin to testcase
    private BlockingQueue<Object> pluginMessageQueue;

    public DefaultMessageHandler() {
        testCaseMessageQueue = new ArrayBlockingQueue<Object>(1000);
        pluginMessageQueue = new ArrayBlockingQueue<Object>(1000);
    }

    @Override
    public void sendMessage2Plugin(Object msg) {
        System.out.println(msg);
        testCaseMessageQueue.offer(msg);
    }

    @Override
    public void sendMessage2TC(Object msg) {
        System.out.println(msg);
        pluginMessageQueue.offer(msg);
    }

    @Override
    public Object getMessageFromTC() {
        Object msg = null;
        try {
            msg = testCaseMessageQueue.take();
        } catch (InterruptedException e) {
        }
        System.out.println(msg);
        return msg;
    }

    @Override
    public Object getMessageFromPlugin() {
        return getMessageFromPlugin(3000);
    }

    @Override
    public Object getMessageFromPlugin(long timeout) {
        Object msg = null;
        try {
            msg = pluginMessageQueue.poll(timeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
        }
        System.out.println(msg);
        return msg;
    }
}
